/** I N C L U D E S **********************************************************/

#include "faa_common.h"
#include "inputprocessing.h"
#include "eep_emulation.h"
#include "usart.h"

/* VARIABLE DECLRATIONS */
unsigned int FAA_BUTTON_PRESS_COUNTER;
unsigned char FAA_BUTTON_COUNT_SEC;
unsigned char GSM_MESSAGE_SENT_FLAG;

/****f* Main/is_sw1_pressed
*  NAME
*    is_sw1_pressed
*  SYNOPSIS
*    unsigned char is_sw1_pressed(void)
*  FUNCTION
*    this routine is to check key1 pressings.
*  INPUTS
*    none
*  RESULT
*   0 if switch is not pressed. 1 if switch is pressed. 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
void FAA_BUTTON_PRESS_INIT(void)
{
	FAA_BUTTON_PRESS_COUNTER = 0;
}
/*********************************************************************************************/

/****f* Main/is_sw1_pressed
*  NAME
*    is_sw1_pressed
*  SYNOPSIS
*    unsigned char is_sw1_pressed(void)
*  FUNCTION
*    this routine is to check key1 pressings.
*  INPUTS
*    none
*  RESULT
*   0 if switch is not pressed. 1 if switch is pressed. 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
void FAA_BUTTON_PRESS_LOOP(void)
{
	if(FAA_BUTTON1_PRESS_CHECK()==1)    // is key 1 pressed
	{                        // yes
		LATBbits.LATB0=1;     // turn ON LED1				  
	}
	if(FAA_BUTTON2_PRESS_CHECK()==1)    // is key 1 pressed
	{                        // yes
		LATBbits.LATB1=1;     // turn ON LED1				  
	}
	if(FAA_BUTTON3_PRESS_CHECK()==1)    // is key 1 pressed
	{                        // yes
		LATBbits.LATB2=1;     // turn ON LED1				  
	}
	if(FAA_BUTTON4_PRESS_CHECK()==1)    // is key 1 pressed
	{                        // yes
		LATBbits.LATB3=1;     // turn ON LED1				  
	}	

//	if (DataEERead(0x06) == 20)
//	{
		FAA_BUTTON_PRESS_COUNTER_RESET();
//	}

}
/*********************************************************************************************/


/****f* Main/is_sw1_pressed
*  NAME
*    is_sw1_pressed
*  SYNOPSIS
*    unsigned char is_sw1_pressed(void)
*  FUNCTION
*    this routine is to check key1 pressings.
*  INPUTS
*    none
*  RESULT
*   0 if switch is not pressed. 1 if switch is pressed. 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
unsigned char FAA_BUTTON1_PRESS_CHECK(void)
{
    if (PORTBbits.RB4==0)        // is SW1 pressed?
    {                            //yes
        FAA_BUTTON_PRESS_START_TIMER();            // wait 10mS for debounce.
        if ((FAA_BUTTON_PRESS_COUNTER>=45) && (PORTBbits.RB4==0))    // is SW1 still has pressed status after 10mS delay?
        {                        // yes, we have key press
		
			GSM_SEND_MSG();
			
            return 1;
        }
    }
    return 0;// if key is not pressed, return 0
}
/*********************************************************************************************/

/****f* Main/is_sw2_pressed
*  NAME
*    is_sw2_pressed
*  SYNOPSIS
*    unsigned char is_sw2_pressed(void)
*  FUNCTION
*    this routine is to check key2 pressings.
*  INPUTS
*    none
*  RESULT
*   0 if switch is not pressed. 1 if switch is pressed. 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
unsigned char FAA_BUTTON2_PRESS_CHECK(void)
{
    if (PORTBbits.RB5==0)        // is SW1 pressed?
    {                            //yes
        FAA_BUTTON_PRESS_START_TIMER();            // wait 10mS for debounce.
        if ((FAA_BUTTON_PRESS_COUNTER>=45) && (PORTBbits.RB5==0))    // is SW1 still has pressed status after 10mS delay?
        {                        // yes, we have key press
            return 1;
        }
    }
    return 0;// if key is not pressed, return 0
}
/*********************************************************************************************/



/****f* Main/is_sw3_pressed
*  NAME
*    is_sw3_pressed
*  SYNOPSIS
*    unsigned char is_sw3_pressed(void)
*  FUNCTION
*    this routine is to check key3 pressings.
*  INPUTS
*    none
*  RESULT
*   0 if switch is not pressed. 1 if switch is pressed. 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
unsigned char FAA_BUTTON3_PRESS_CHECK(void)
{
    if (PORTBbits.RB6==0)        // is SW1 pressed?
    {                            //yes
        FAA_BUTTON_PRESS_START_TIMER();            // wait 10mS for debounce.
        if ((FAA_BUTTON_PRESS_COUNTER>=45) && (PORTBbits.RB6==0))    // is SW1 still has pressed status after 10mS delay?
        {                        // yes, we have key press
            return 1;
        }
    }
    return 0;// if key is not pressed, return 0
}
/*********************************************************************************************/


/****f* Main/is_sw4_pressed
*  NAME
*    is_sw4_pressed
*  SYNOPSIS
*    unsigned char is_sw4_pressed(void)
*  FUNCTION
*    this routine is to check key4 pressings.
*  INPUTS
*    none
*  RESULT
*   0 if switch is not pressed. 1 if switch is pressed. 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
unsigned char FAA_BUTTON4_PRESS_CHECK(void)
{
    if (PORTBbits.RB7==0)        // is SW1 pressed?
    {                            //yes
        FAA_BUTTON_PRESS_START_TIMER();            // wait 10mS for debounce.
        if ((FAA_BUTTON_PRESS_COUNTER>=45) && (PORTBbits.RB7==0))    // is SW1 still has pressed status after 10mS delay?
        {                        // yes, we have key press
			
            return 1;
        }
    }
    return 0;// if key is not pressed, return 0
}
/*********************************************************************************************/


/****f* Main/InterruptVectorLow 
*  NAME
*    InterruptVectorLow 
*  SYNOPSIS
*    void InterruptVectorLow (void)
*  FUNCTION
*   Low priority interrupt routine
*  INPUTS
*    none
*  RESULT
*   none 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
#pragma code InterruptVectorHigh = 0x8
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh 
  _endasm
}
/*********************************************************************************************/


/****f* Main/InterruptVectorLow 
*  NAME
*    InterruptVectorLow 
*  SYNOPSIS
*    void InterruptVectorLow (void)
*  FUNCTION
*   Low priority interrupt routine
*  INPUTS
*    none
*  RESULT
*   none 
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh ()
{	
	if(INTCONbits.TMR0IE && INTCONbits.TMR0IF)
   	{
		if (FAA_BUTTON_PRESS_COUNTER >= 30000)
		{
			FAA_BUTTON_PRESS_COUNTER = 0;
		}
		FAA_BUTTON_PRESS_COUNTER++;
	/*	FAA_BUTTON_COUNT_SEC = FAA_BUTTON_PRESS_COUNTER/45;
		DataEEWrite(FAA_BUTTON_COUNT_SEC,0x06);
		if(FAA_BUTTON_COUNT_SEC >= 100)
		{
			FAA_BUTTON_COUNT_SEC = 0;
		}*/
		INTCONbits.TMR0IF=0;
	}		
}
/*********************************************************************************************/


/****f* FAA/FAA_BUTTON_PRESS_START_TIMER
*  NAME
*    FAA_BUTTON_PRESS_START_TIMER
*  SYNOPSIS
*    void FAA_BUTTON_PRESS_START_TIMER(void)
*  FUNCTION
*    This routine starts the timer
*  INPUTS
*    none
*  RESULT
*   Timer is started
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
void FAA_BUTTON_PRESS_START_TIMER(void)
{
//Setup Timer0
	T0CONbits.T0PS0=1; //Prescaler is divide by 256	
	T0CONbits.T0PS1=1;
	T0CONbits.T0PS2=1;
	
	T0CONbits.PSA=0;      //Timer Clock Source is from Prescaler	
	T0CONbits.T0CS=0;     //Prescaler gets clock from FCPU (5MHz)	
	T0CONbits.T08BIT=1;   //8 BIT MODE
	
	INTCONbits.TMR0IE=1;   //Enable TIMER0 Interrupt
	INTCONbits.PEIE=1;     //Enable Peripheral Interrupt	
	INTCONbits.GIE=1;      //Enable INTs globally
	
	T0CONbits.TMR0ON=1;      //Now start the timer!
}
/*********************************************************************************************/


/****f* FAA/FAA_BUTTON_PRESS_STOP_TIMER
*  NAME
*    FAA_BUTTON_PRESS_STOP_TIMER
*  SYNOPSIS
*    void FAA_BUTTON_PRESS_STOP_TIMER(void)
*  FUNCTION
*    This routine stops the timer
*  INPUTS
*    none
*  RESULT
*   Timer is started
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
void FAA_BUTTON_PRESS_STOP_TIMER(void)
{
	T0CONbits.T0PS0=0; 	
	T0CONbits.T0PS1=0;
	T0CONbits.T0PS2=0;
	
	T0CONbits.PSA=0;	
	T0CONbits.T0CS=0; 	
	T0CONbits.T08BIT=0;   
	
	INTCONbits.TMR0IE=0;   
	INTCONbits.PEIE=0; 	
	INTCONbits.GIE=0;      
	
	T0CONbits.TMR0ON=0;   
}
/*********************************************************************************************/


/****f* FAA/FAA_BUTTON_PRESS_STOP_TIMER
*  NAME
*    FAA_BUTTON_PRESS_STOP_TIMER
*  SYNOPSIS
*    void FAA_BUTTON_PRESS_STOP_TIMER(void)
*  FUNCTION
*    This routine stops the timer
*  INPUTS
*    none
*  RESULT
*   Timer is started
*  NOTES
*    
*  SEE ALSO
*
******
* You can use this space for remarks that should not be included
* in the documentation.
*/
void FAA_BUTTON_PRESS_COUNTER_RESET(void)
{
	if (FAA_BUTTON_PRESS_COUNTER >= 450)
	{
		LATBbits.LATB0 = 0;
		LATBbits.LATB1 = 0;
		LATBbits.LATB2 = 0;
		LATBbits.LATB3 = 0;
		
		FAA_BUTTON_PRESS_STOP_TIMER();
		FAA_BUTTON_PRESS_COUNTER = 0;
		GSM_MESSAGE_SENT_FLAG = 0;
	//	DataEEWrite(0x00,0x06);
	}
}
/*********************************************************************************************/


void GSM_SEND_MSG(void)
{
	if (GSM_MESSAGE_SENT_FLAG == 0)
	{
		putrs1USART("AT+CMGS=\"004917655374352\"\r\n");
		delay_ms(100);
		putrs1USART("Sangi Mangi this is our message");
		putc1USART(26);
		GSM_MESSAGE_SENT_FLAG = 1;
	}
}